#include <QApplication>
#include<QWidget>
#include<QPixmap>
#include<QMap>
#include<QPainter>
#include<QPaintEvent>
#include<QRandomGenerator>
#include<QtGlobal>
#include<QMouseEvent>
#include<QtMultimedia>
#include<QSoundEffect>

const int ROW_COUNT=9;
const int COLUMN_COUNT=9;
const int BLOCK_WIDTH=30;
const int BLOCK_HEIGHT=30;
const int BOOM_COUNT=QRandomGenerator::global()->bounded(30);
const int WINDOW_WIDTH=ROW_COUNT*BLOCK_WIDTH;
const int WINDOW_HEIGHT=COLUMN_COUNT*BLOCK_HEIGHT;
enum BLOCK{
    ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX
    ,SEVEN,EIGHT,BOOM,UNKNOWN,FLAG
};
QMap<BLOCK,QPixmap>map;
inline bool isNum(BLOCK b){
    return b==ONE||b==TWO||b==THREE||b==FOUR||
           b==FIVE||b==SIX||b==SEVEN||b==EIGHT;
}
class Window:public QWidget
{public:
    BLOCK data[ROW_COUNT][COLUMN_COUNT];
    BLOCK view[ROW_COUNT][COLUMN_COUNT];
    bool gameOver=false;
    bool youWin=false;
    Window(){
        setWindowTitle("扫雷->ikun");
        setFixedSize(WINDOW_WIDTH,WINDOW_HEIGHT);
        setFont(QFont("微软雅黑",20));
        setWindowIcon(QIcon(":/new/prefix1/img/lei.png"));
        for(int i=0;i<ROW_COUNT;i++)
        {
            for(int j=0;j<COLUMN_COUNT;j++)
            {
                data[i][j]=ZERO;
                view[i][j]=UNKNOWN;
            }
        }
        for(int i=0;i<BOOM_COUNT;i++)
        {
            int v=QRandomGenerator::global()->generate()%(ROW_COUNT*COLUMN_COUNT);
            int x=v/ROW_COUNT,y=v%ROW_COUNT;
            data[x][y]=BOOM;
        }
        for(int i=0;i<ROW_COUNT;i++)
        {
            for(int j=0;j<COLUMN_COUNT;j++)
            {
                if(data[i][j]==BOOM) continue;
                int sum=0;
                for(int dx=-1;dx<=1;dx++)
                {
                    for(int dy=-1;dy<=1;dy++)
                    {
                        if(i+dx>=0&&i+dx<ROW_COUNT&&j+dy>=0&&j+dy<COLUMN_COUNT&&data[i+dx][j+dy]==BOOM)
                        {
                            ++sum;
                        }
                    }
                }
                switch(sum){
                case 0:data[i][j]=ZERO;break;
                case 1:data[i][j]=ONE;break;
                case 2:data[i][j]=TWO;break;
                case 3:data[i][j]=THREE;break;
                case 4:data[i][j]=FOUR;break;
                case 5:data[i][j]=FIVE;break;
                case 6:data[i][j]=SIX;break;
                case 7:data[i][j]=SEVEN;break;
                case 8:data[i][j]=EIGHT;break;
                }
            }
        }
             };
    void dfs(int i,int j){
        if(gameOver||youWin)return;
        if(view[i][j]!=UNKNOWN)return ;
        view[i][j]=data[i][j];
                for(int dx=-1;dx<=1;dx++)
                {
                    for(int dy=-1;dy<=1;dy++)
                    {
                        if(dx==0&&dy==0)continue;
                        int nx=i+dx,ny=j+dy;
                        if(nx>=0&&nx<ROW_COUNT&&ny>=0&&ny<COLUMN_COUNT)
                        {

                            if(isNum(data[nx][ny])) {

                               view[nx][ny]=data[nx][ny];
                            }else if(data[nx][ny]==ZERO)
                            {dfs(nx,ny);}
                        }
                    }
                }
    }
    void paintEvent(QPaintEvent *event)override{
        Q_UNUSED(event)
        QPainter painter(this);
        if(youWin){
            painter.fillRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT,Qt::gray);
            painter.drawText(0,0,WINDOW_WIDTH,WINDOW_HEIGHT,Qt::AlignCenter,"厉不厉害你坤哥");
            return;
        }

        for(int i=0;i<ROW_COUNT;i++)
        {
            for(int j=0;j<COLUMN_COUNT;j++)
            {
                painter.drawPixmap(BLOCK_WIDTH*i,BLOCK_HEIGHT*j,gameOver? map[data[i][j]]:map[view[i][j]]);
            }
        }
             }
    bool Win(){
        for(int i=0;i<ROW_COUNT;i++){
            for(int j=0;j<COLUMN_COUNT;j++)
            {
                if((view[i][j]==UNKNOWN||view[i][j]==FLAG)&&data[i][j]!=BOOM) return false;
            }
        }
        return true;
    }
    void mousePressEvent(QMouseEvent *event) override{
        int x=event->pos().x()/BLOCK_WIDTH;int y=event->pos().y()/BLOCK_HEIGHT;
        if(event->button()==Qt::LeftButton&&view[x][y]==UNKNOWN){
            if(data[x][y]!=BOOM)
            {
            QSoundEffect*start=new QSoundEffect(this);
            start->setSource(QUrl::fromLocalFile(":/u8mbc-urrxa.wav"));
            start->play();
            }
            if(data[x][y]==BOOM){
                gameOver=true;
                repaint();
                QSoundEffect*startsound=new QSoundEffect(this);
                startsound->setSource(QUrl::fromLocalFile(":/py7x8-kx4z7.wav"));
                startsound->play();
            }

            dfs(x,y);
            youWin=Win();
            repaint();
            if(youWin)
            {
                QSoundEffect*startsound=new QSoundEffect(this);
                startsound->setSource(QUrl::fromLocalFile(":/rz38y-uz54i.wav"));
                startsound->play();
            }
        }

        else if(event->button()==Qt::RightButton&&(view[x][y]==UNKNOWN||view[x][y]==FLAG)){
            if(view[x][y]==UNKNOWN)
            {
                QSoundEffect*startsound=new QSoundEffect(this);
                startsound->setSource(QUrl::fromLocalFile(":/d1vau-34kin.wav"));
                startsound->play();
            }
            if(view[x][y]==FLAG)
            {
                QSoundEffect*startsound=new QSoundEffect(this);
                startsound->setSource(QUrl::fromLocalFile(":/82wvo-aeqob.wav"));
                startsound->play();
            }
            view[x][y]=view[x][y]==FLAG?UNKNOWN: FLAG;
            repaint();
    }
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    auto *tiles=new QPixmap(":/C:/Users/hp/Pictures/Screenshots/1.jpg");
    map.insert(ZERO,tiles->copy(0,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(ONE,tiles->copy(30*1-1,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(TWO,tiles->copy(30*2-2,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(THREE,tiles->copy(30*3-2,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(FOUR,tiles->copy(30*4-3,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(FIVE,tiles->copy(30*5-4,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(SIX,tiles->copy(30*6-4,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(SEVEN,tiles->copy(30*7-5,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(EIGHT,tiles->copy(30*8-5,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(BOOM,tiles->copy(30*9-6.5,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(UNKNOWN,tiles->copy(30*10-7,0,BLOCK_WIDTH,BLOCK_HEIGHT));
    map.insert(FLAG,tiles->copy(30*11-8,0,BLOCK_WIDTH,BLOCK_HEIGHT));

    Window window;
    window.show();

    return a.exec();
}
